package com.zengfa.task.server.dao.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.jdbc.core.simple.JdbcClient.StatementSpec;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.zengfa.task.api.enums.JobStatus;
import com.zengfa.task.server.dao.JobNodeDao;
import com.zengfa.task.server.domain.entity.JobNode;

@Repository
@ConditionalOnBean(JdbcClient.class)
public class JobNodeDaoJdbcImpl implements JobNodeDao {

	@Autowired
	private JdbcClient jdbcClient;
	
	@Override
	public void save(JobNode jobNode) {
		String sql = "insert into job_node(id,job_id,node_order,node_code,node_name,run_type,exec_ip,status,"
				+ "start_time,end_time,insert_time,update_time,insert_by,update_by) values(:id,:jobId,:nodeOrder,:nodeCode,:nodeName,:runType,:execIp,:status,"
				+ ":startTime,:endTime,:insertTime,:updateTime,:insertBy,:updateBy)";
		int i = jdbcClient.sql(sql)
			.param("id",jobNode.getId())
			.param("jobId",jobNode.getJobId())
			.param("nodeOrder",jobNode.getNodeOrder())
			.param("nodeCode",jobNode.getNodeCode())
			.param("nodeName",jobNode.getNodeName())
			.param("runType",jobNode.getRunType())
			.param("execIp",jobNode.getExecIp())
			.param("status",jobNode.getStatus().name())
			.param("startTime",jobNode.getStartTime())
			.param("endTime",jobNode.getEndTime())
			.param("insertTime",jobNode.getInsertTime())
			.param("updateTime",jobNode.getUpdateTime())
			.param("insertBy",jobNode.getInsertBy())
			.param("updateBy",jobNode.getUpdateBy())
			.update();
		if (i < 1) {
			throw new RuntimeException("保存任务节点失败");
		}
	}

	@Override
	public List<JobNode> listByJobId(String jobId) {
		String sql = "select * from job_node where job_id=? order by node_order asc";
		return jdbcClient.sql(sql).param(jobId).query(JobNode.class).list();
	}

	@Override
	public void updateById(JobNode jobNode) {
		String sql = "update job_node set node_order=:nodeOrder,node_code=:nodeCode,node_name=:nodeName,run_type=:runType,exec_ip=:execIp,"
				+ "status=:status,start_time=:startTime,end_time=:endTime,update_time=:updateTime,update_by=:updateBy where id=:id";
		int i = jdbcClient.sql(sql)
			.param("id",jobNode.getId())
			.param("nodeOrder",jobNode.getNodeOrder())
			.param("nodeCode",jobNode.getNodeCode())
			.param("nodeName",jobNode.getNodeName())
			.param("runType",jobNode.getRunType())
			.param("execIp",jobNode.getExecIp())
			.param("status",jobNode.getStatus().name())
			.param("startTime",jobNode.getStartTime())
			.param("endTime",jobNode.getEndTime())
			.param("updateTime",jobNode.getUpdateTime())
			.param("updateBy",jobNode.getUpdateBy())
			.update();
	}
	
	@Override
	public boolean updateByIdAndStatus(JobNode jobNode, JobStatus beforeStatus) {
		StringBuffer sb = new StringBuffer();
		sb.append("update job_node set update_time=:updateTime,update_by=:updateBy");
		if (jobNode.getStatus() != null) {
			sb.append(",status=:status");
		}
		if(StringUtils.hasText(jobNode.getExecIp())) {
			sb.append(",exec_ip=:execIp");
		}
		if(StringUtils.hasText(jobNode.getMessage())) {
			sb.append(",message=:message");
		}
		if(jobNode.getStartTime() != null) {
			sb.append(",start_time=:startTime");
		}
		if(jobNode.getEndTime() != null) {
			sb.append(",end_time=:endTime");
		}
		sb.append(" where id=:id and status=:beforeStatus");
		StatementSpec ss = jdbcClient.sql(sb.toString());
		ss.param("updateTime",jobNode.getUpdateTime());
		ss.param("updateBy",jobNode.getUpdateBy());
		ss.param("id",jobNode.getId());
		ss.param("beforeStatus",beforeStatus.name());
		if (jobNode.getStatus() != null) {
			ss.param("status",jobNode.getStatus().name());
		}
		if(StringUtils.hasText(jobNode.getExecIp())) {
			ss.param("execIp",jobNode.getExecIp());
		}
		if(StringUtils.hasText(jobNode.getMessage())) {
			ss.param("message",jobNode.getMessage());
		}
		if(jobNode.getStartTime() != null) {
			ss.param("startTime",jobNode.getStartTime());
		}
		if(jobNode.getEndTime() != null) {
			ss.param("endTime",jobNode.getEndTime());
		}
		int i = ss.update();
		if(i > 0) {
        	return true;
        }
        return false;
	}

}
